/*
 * Carrello
 */
package carrello.impl.boundary;

import carrello.model.entities.Company;
import carrello.model.entities.Product;
import carrello.model.entitydao.CompanyDAO;
import carrello.util.CollectionUtil;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionSupport;
import java.util.HashMap;

import java.util.Map;
import java.util.Set;

/**
 * This is the abstract boundary class that all other boundary classes extend.
 * @author Michael Cotterell <mepcotterell@gmail.com>
 */
public abstract class AbstractUI extends ActionSupport {
    
    /**
     * 
     * @return Map the current HTTP Session map.
     */
    public static Map getSession () {
               
        return ActionContext.getContext().getSession();
    }
    
    public static Map getApplication () {
        return ActionContext.getContext().getApplication();
    }
    private int cartCount = 0;
    private static final String USERNAME   = "username";
    private static final String ISLOGGEDIN = "isLoggedIn";
    private static final String ROLE       = "role";
    
    private static final String TRUE       = Boolean.toString(true);
    private static final String FALSE      = Boolean.toString(false);
    
    protected boolean sessionIsLoggedIn    = false;
    protected String  sessionRole          = "";
    protected String  sessionUsername      = "Not Logged In!";
    
    private String    actionName           = "";   
    private String    companyName          = "YourCompany";
    
    /**
     * Returns the value of the isLoggedIn HTTP Session variable.
     * @return boolean true if isLoggedIn session variable is true, false otherwise.
     */
    public boolean getSessionIsLoggedIn () {
        
        if (getSession().containsKey(ISLOGGEDIN)) {
            if (getSession().get(ISLOGGEDIN).equals(TRUE)) {
                sessionIsLoggedIn = true;
                return true;
            }
        }

        sessionIsLoggedIn = false;
        return false;
    }
    
    /**
     * Returns the value of the role HTTP Session variable.
     * @return String value of the role session variable
     */
    public String getSessionRole () {

        if (getSession().containsKey(ROLE)) {
            if (getSession().get(ROLE) != null) {
                sessionRole = (String) getSession().get(ROLE);
                return sessionRole;
            }
        }
        
        sessionRole = "";
        return sessionRole;
    }
    
    public void setSessionCart(Map<Integer, Integer> productInfo)
    {
        int count = CollectionUtil.getValueCount(productInfo.values());
        cartCount += count;

        if (getSessionIsLoggedIn()) {
                getSession().put(getSessionUsername(), productInfo);
        } else {
            setSessionIsLoggedIn(false);
        }
        
    } // setProductToSession
    
     public Map<Integer, Integer> getSessionCart()
    {
        Map<Integer, Integer> p = null;
        if (getSession().containsKey(getSessionUsername())) {
             return (Map<Integer, Integer>)getSession().get(getSessionUsername());
        }
        return null;
    }
    /**
     * Returns the value of the username HTTP Session variable.
     * @return String value of the username session variable
     */
    public String getSessionUsername () {
        if (getSession().containsKey(USERNAME)) {
            if (getSession().get(USERNAME) != null) {
                
                sessionUsername = (String) getSession().get(USERNAME);
                
                return sessionUsername;
            }
        }
        sessionUsername = "Not Logged In";
        return sessionUsername;
    }
    
    /**
     * Sets the value of the isLoggedIn HTTP Session variable.
     * @param isLoggedIn 
     */
    public void setSessionIsLoggedIn (boolean isLoggedIn) {
        if (isLoggedIn) {

            getSession().put(ISLOGGEDIN, TRUE);
            
        } else {
            getSession().put(ISLOGGEDIN, FALSE);
        }
    }
    
    /**
     * Sets the value of the role HTTP Session variable.
     * @param role 
     */
    public void setSessionRole (String role) {
        getSession().put(ROLE, role);
    }
    
    /**
     * Sets the value of the username HTTP Session variable.
     * @param username 
     */
    public void setSessionUsername (String username) {
                

        getSession().put(USERNAME, username);
    }
    
    /**
     * Removes the isLoggedIn HTTP Session variable.
     */
    public void removeSessionIsLoggedIn () {
        removeSessionRole();
        removeSessionUsername();
        getSession().remove(ISLOGGEDIN);
    }
    
    /**
     * Removes the role HTTP Session variable.
     */
    public void removeSessionRole () {
        getSession().remove(ROLE);
    }
    
    /**
     * Removes the username HTTP Session variable.
     */
    public void removeSessionUsername () {
        getSession().remove(USERNAME);
    }

    /**
     * @return the companyName
     */
    public String getCompanyName() {
        CompanyDAO companyDAO = new CompanyDAO();
        Company company = companyDAO.getCompany();
        //Company company = Company.getCompanyByName();
        setCompanyName(company.getName());
        return companyName;
    }

    /**
     * @param companyName the companyName to set
     */
    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    /**
     * @return the actionName
     */
    public String getActionName() {

        this.actionName = ActionContext.getContext().getName();
        return actionName;
    }

    /**
     * @param actionName the actionName to set
     */
    public void setActionName(String actionName) {
        this.actionName = actionName;
    }

    public int getCartCount() {
        if (getSession().containsKey(getSessionUsername())) {
            return CollectionUtil.getValueCount(((Map<Integer, Integer>)getSession().get(getSessionUsername())).values());
        }
        else return 0;
    }

    public void setCartCount(int cartCount) {
        this.cartCount = cartCount;
    }
    
} // AbstractUI
